﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NetfractionHub.Network.Protocol.BASE.Factory
{
    public class FactoryMSG : ProtocolMessageFactory
    {
        public enum ParameterEnum : int
        {
            SessionID = 0,
            TargetID = 1,
            Text = 2,
            PrivateMessage = 3,
            METext = 4
        }

        public enum METextTypesEnum : int
        {
            IRCNickText = 1
        }

        public override ProtocolMessage Create(ProtocolMessage.MessageTypeEnum messageType, Dictionary<int, object> parameters)
        {
            ProtocolMessage proto = new ProtocolMessage(messageType, ProtocolFeatureBASE.FeatureActions.ActionMSG);
            
            proto.SessionId = parameters[(int)ParameterEnum.SessionID].ToString();

            if (messageType == ProtocolMessage.MessageTypeEnum.Echo || messageType == ProtocolMessage.MessageTypeEnum.Direct)
                proto.TargetSessionId = parameters[(int)ParameterEnum.TargetID].ToString();

            if (!parameters.ContainsKey((int)ParameterEnum.Text))
                return null;

            proto.Parameters.Add(new ProtocolParameter(parameters[(int)ParameterEnum.Text].ToString()));

            foreach (ParameterEnum en in parameters.Keys)
            {
                if (en != ParameterEnum.Text && en != ParameterEnum.SessionID && en != ParameterEnum.TargetID)
                    proto.Parameters.Add(new ProtocolParameter(GetParameterCodeForParameter((ParameterEnum)en), parameters[(int)en].ToString()));
            }

            return proto;
        }

        private string GetParameterCodeForParameter(ParameterEnum param)
        {
            switch (param)
            {
                case ParameterEnum.Text:
                    return "Text";
                case ParameterEnum.PrivateMessage:
                    return "PM";
                case ParameterEnum.METext:
                    return "ME";
                default:
                    return "";
            }
        }
    }
}
